package 力扣日常刷题.木22一月.第04天0111;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/1/11
 * @description
 */
public class Demo01回溯中等允许选择元素的组合 {


    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(candidates,0,target,0,new ArrayList<Integer>());
        return ans;
    }

    void dfs(int[] candidates, int index,int target,int sum,List<Integer> list){
        if(target == sum){
            ans.add(new ArrayList<>(list));
            return ;
        }
        if(sum > target || index >= candidates.length){
            return ;
        }

        list.add(candidates[index]);
        sum += candidates[index];
        // 加上多次
        dfs(candidates,index,target,sum,list);
        sum -= list.remove(list.size()-1);
        dfs(candidates,index+1,target,sum,list);

    }

}
